package com.redlight.token;

import com.redlight.utils.StringUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author chao
 * @Title: Audience
 * @date 2019-01-24 21:32
 */
public class Audience {
    private static final String CONF_FILE = "config/apiAuth.properties";
    private final String tokenType;
    private String name;
    private String clientId;
    private Integer expiresSecond;
    private String base64Secret;
    private boolean authenticate;
    private String jdbcAuthnSearchTable;
    private String jdbcAuthnSearchParamField;
    private String jdbcAuthnSearchField;

    public static Audience getInstance() {
        return Audience.AudienceHolder.INSTANCE;
    }

    private Audience() {
        this.tokenType = "Bearer";
        this.expiresSecond = -1;
        Properties prop = new Properties();
        InputStream is = null;

        try {
            ResourceLoader resourceLoader = new DefaultResourceLoader();
            Resource resource = resourceLoader.getResource("config/apiAuth.properties");
            is = resource.getInputStream();
            prop.load(is);
        } catch (IOException var8) {
            var8.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }

        this.name = prop.getProperty("name");
        this.clientId = prop.getProperty("clientId");
        this.base64Secret = prop.getProperty("base64Security");
        if (prop.containsKey("expiresSecond") && StringUtil.isNotBlank(prop.getProperty("expiresSecond"))) {
            this.expiresSecond = Integer.parseInt(prop.getProperty("expiresSecond"));
        }

        if (prop.containsKey("authenticate") && StringUtil.isNotBlank(prop.getProperty("authenticate"))) {
            this.authenticate = Boolean.parseBoolean(prop.getProperty("authenticate"));
        }

        this.jdbcAuthnSearchTable = prop.getProperty("jdbc.authn.search.table");
        this.jdbcAuthnSearchParamField = prop.getProperty("jdbc.authn.search.paramFiled");
        this.jdbcAuthnSearchField = prop.getProperty("jdbc.authn.search.field");
    }

    public String getName() {
        return this.name;
    }

    public String getClientId() {
        return this.clientId;
    }

    public int getExpiresSecond() {
        return this.expiresSecond;
    }

    public String getBase64Secret() {
        return this.base64Secret;
    }

    public boolean isAuthenticate() {
        return this.authenticate;
    }

    public String getTokenType() {
        return "Bearer";
    }

    public String getJdbcAuthnSearchTable() {
        return this.jdbcAuthnSearchTable;
    }

    public String getJdbcAuthnSearchParamField() {
        return this.jdbcAuthnSearchParamField;
    }

    public String getJdbcAuthnSearchField() {
        return this.jdbcAuthnSearchField;
    }

    private static class AudienceHolder {
        private static final Audience INSTANCE = new Audience();

        private AudienceHolder() {
        }
    }
}
